//上传图片
function uploadImg(btnSelector, dir, cb) {
    var accept = "image/jpeg,image/png,image/gif";
    uploadFun(btnSelector, dir, accept, cb);
}

function uploadFun(btnSelector, dir, accept, cb) {
    //添加上传控件
    var file = '<input type="file" name="file" class="hidden" dir="' + dir + '" accept="' + accept + '">';
    $(btnSelector).after(file);
    //触发上传控件
    $(btnSelector).click(function () {
        $(this).next().trigger("click");
    })
    //上传控件绑定切换事件
    $(btnSelector).next().change(function () {
        var formData = new FormData();
        formData.append("file", $(btnSelector).next()[0].files[0]);
        formData.append("dir", $(btnSelector).next().attr("dir"));
        uploadFile("/upload/img", formData, function (re) {
            if (re.status) {
                cb(re.data);
            } else {
                msgAlert(re.message)
            }
        })
        $(btnSelector).next().val("");
    })
}

function uploadFile(url, data, cb) {
    $.ajax({
        url: url,
        type: "post",
        data: data,
        dataType: "json",
        cache: false,
        processData: false,
        contentType: false,
        success: cb,
        error: function () {
            console.log("上传失败")
        }
    });
}